package com.tamecode.beanfactory;

import com.tamecode.reflect.Car;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;

/**
 * @Author: LiQiongchao
 * @Date: 2021/8/5 20:58
 */
public class BeanFactoryTest {

    @Test
    public void getBean() throws IOException {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath:beans.xml");
        // file:/D:/WorkSpaces/practise-projects/spring-4.x-learn/chapter04-Ioc/target/classes/beans.xml
        Resource res;
        System.out.println((res = resources[0]).getURL());

        // 已废弃，不建议使用
//        BeanFactory bf = new XmlBeanFactory(res);
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
        reader.loadBeanDefinitions(res);

        System.out.println("init bean factory");
        Car car = factory.getBean(Car.class);
        System.out.println("car bean is ready for use.");
        car.introduce();
        /*
        init bean factory
        car bean is ready for use.
        Car{brand='BYD', color='white', maxSpeed=150}
         */
    }

}
